package com.duguo.dynamicmvc.support.servlet;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.web.context.ContextLoader;

public class WebApplicationBasedFilter extends AbstractDynamicMVC implements Filter{

	private ContextLoader _contextLoader;

	public void init(FilterConfig filterConfig) throws ServletException {
		super.init(filterConfig.getServletContext());
	}


	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
		service(req, res);
	}
	
	protected ListableBeanFactory createBeanFactory() {
		return getContextLoader().initWebApplicationContext(getServletContext());
	}


	protected void destroyBeanFactory() {		
		getContextLoader().closeWebApplicationContext(getServletContext());
	}


	public ContextLoader getContextLoader() {
		if(_contextLoader==null){
			_contextLoader =new ContextLoader();
		}
		return _contextLoader;
	}


	public void setContextLoader(ContextLoader contextLoader) {
		_contextLoader = contextLoader;
	}
	
	
	
}
